layui.use(['table', 'admin', 'ax', 'func','upload','form','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var upload = layui.upload;
    var form = layui.form;
    var laydate = layui.laydate;



    //渲染时间选择框
    laydate.render({
        elem: '#zdrq',
        trigger: 'click',
        // max: Feng.currentDate()
    });

    /**
     * 管理
     */
    var BtyJtgz = {
        tableId: "btyJtgzTable"
    };

    form.on('select(flag)', function (data) {
        BtyJtgz.search();
    });

    /**
     * 初始化表格的列
     */
    BtyJtgz.initColumn = function () {
        return [[
            //{type: 'checkbox'},
            {field: 'id', hide: true, title: ''},
            {align: 'center', type: "numbers", title: '序号'},
            {field: 'code',  title: '用友大区编号'},
            {field: 'region', title: '地区'},
            {field: 'dm', title: '店名'},
            {field: 'amount', title: '计提工资'},
            //{field: 'flag', sort: true, title: '1成功'},
            {field: 'errormsg', title: '失败原因',width:600},
            //{align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    BtyJtgz.search = function () {
        var queryData = {};

        queryData['dm'] = $('#dm').val();
        queryData['flag'] = $('#flag').val();

        table.reload(BtyJtgz.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 导入Excel
     */
    var loadIndex;
    upload.render({
        elem: '#btnExcel'
        , url: '/btyJtgz/import'
        , accept: "file"
        , exts: 'xls|xlsx|xlsm|xlt|xltx|xltm'
        , before: function () {
            loadIndex = layer.load('正在导入...', 2, {shade: [0.15, '#ccc']});
            this.data = {'zdrq': $('#zdrq').val()};//关键代码
        }
        , done: function (res) {
            if (res.state == 0) {
                Feng.error("导入失败!" + res.errmsg);
            }
            if (res == true) {
                alert("导入成功!");
                Feng.success("导入成功!");
                window.location.reload();
            }
            if (res == false) {
                Feng.error("导入失败!");
                window.location.reload();
            }
            layer.close(loadIndex);
        }
    });

    /**
     * 弹出添加对话框
     */
    BtyJtgz.openAddDlg = function () {
        func.open({
            title: '添加',
            content: Feng.ctxPath + '/btyJtgz/add',
            tableId: BtyJtgz.tableId
        });
    };

     /**
      * 点击编辑
      *
      * @param data 点击按钮时候的行数据
      */
      BtyJtgz.openEditDlg = function (data) {
          func.open({
              title: '修改',
              content: Feng.ctxPath + '/btyJtgz/edit?id=' + data.id,
              tableId: BtyJtgz.tableId
          });
      };


    /**
     * 导出excel按钮
     */
    BtyJtgz.exportExcel = function () {
        //获取导出Excel的数据
        var ajax = new $ax(Feng.ctxPath + "/btyJtgz/listSearchExport");
        var dm = $("#dm").val();
        var flag = $("#flag").val();
        ajax.set("dm", dm);
        ajax.set("flag", flag);
        var result = ajax.start();
        table.exportFile(tableResult.config.id, result, 'xls');
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    BtyJtgz.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/btyJtgz/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(BtyJtgz.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BtyJtgz.tableId,
        url: Feng.ctxPath + '/btyJtgz/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: BtyJtgz.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BtyJtgz.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

    BtyJtgz.openAddDlg();

    });

    // 导出excel
    $('#btnExp').click(function () {
        BtyJtgz.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + BtyJtgz.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            BtyJtgz.openEditDlg(data);
        } else if (layEvent === 'delete') {
            BtyJtgz.onDeleteItem(data);
        }
    });
});
